<?php
// .-----------------------------------------------------------------------------------
// | WE TRY THE BEST WAY 杭州博也网络科技有限公司
// |-----------------------------------------------------------------------------------
// | Author: 青 <99701759@qq.com>
// | Copyright (c) 2013-2016, http://www.itboye.com. All Rights Reserved.
// |-----------------------------------------------------------------------------------
// 微信用户表中的信息在$this->userinfo
// 用户newmember表的信息在$this->newmember中
// 用户角色表的信息在$this->role中
namespace app\web\controller;

use app\src\base\helper\ConfigHelper;
use app\web\api\UserApi;
use app\src\user\action\LoginAction;
use app\src\user\logic\MemberConfigLogic;
use app\src\banners\action\BannersQueryAction;
class Index extends Weixin{


    protected function _initialize() {
      //  $this->redirect('http://api.ycyb.8raw.com/static/weixin');
        parent::_initialize();
        if (session("?global_user")) {
            $this->userinfo = session("global_user");
            if(!empty($this->userinfo['info']['openid'])){
                $openid=$this->userinfo['info']['openid'];
            }else{
                $openid = 'xx';
            }

            $result = (new MemberConfigLogic())->getInfo(['wxopenid'=>$openid]);
            if($result['status']&&!empty($result['info'])){
                $action = new LoginAction();
                $uid=$result['info']['uid'];
                $result = $action->loginByUID($uid,'device_id_um','pc','role_user');
                if($result['status']) {
                    session("memberinfo", $result['info']);
                }
            }else{
                session_destroy();
                $invite_code = $this->_param('invite_code');
                if(!empty($invite_code)){
                    $this->redirect('login/register_auto',['invite_code'=>$invite_code]);
                }else{
                    $this->redirect('login/register_auto',['invite_code'=>'itboye']);
                }

            }
        }else{
            $url = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
          //  dump($url);exit;
            $this->getWxuser($url);
            $openid=$this->userinfo['info']['openid'];
         //   dump($this->userinfo);
            $result = (new MemberConfigLogic())->getInfo(['wxopenid'=>$openid]);
            if($result['status']&&!empty($result['info'])){
                $action = new LoginAction();
                $uid=$result['info']['uid'];
                $result = $action->loginByUID($uid,'device_id_um','pc','role_user');
                if($result['status']) {
                    session("memberinfo", $result['info']);
                }
            }else{
                session_destroy();
                $invite_code = $this->_param('invite_code');
                if(!empty($invite_code)){
                    $this->redirect('login/register_auto',['invite_code'=>$invite_code]);
                }else{
                    $this->redirect('login/register_auto',['invite_code'=>'itboye']);
                }
            }
        }
    }

    /*
     * 首页
     * */
    public function index(){
        $this->assignTitle('有车有保');
        $result = (new BannersQueryAction())->query(6079);
        $result2 = (new BannersQueryAction())->query(6243);
       //   dump($result);exit;
        $SCROLL_TEXT = ConfigHelper::getValue('SCROLL_TEXT');
        if(is_null($SCROLL_TEXT)) $SCROLL_TEXT = [];

        $this->assign('scroll_time', 3 * count($SCROLL_TEXT));
        $this->assign('scroll_text_per', count($SCROLL_TEXT)>0 ?100 / count($SCROLL_TEXT) : 0);
        $this->assign('SCROLL_TEXT', $SCROLL_TEXT);
        $this->assign('pics', $result['info']);

        //  dump($this->userinfo);exit;
        $this->assign('pics2', $result2['info']);
        return $this->fetch();
    }
    public function protocal(){
        $this->assignTitle('掌握科技');

        return $this->fetch();
    }


    public function protocaldetail(){

        return $this->fetch();
    }


//    public function register()
//    {
//        if (session("?memberinfo")) {
//            $this->redirect('index/index');
//        }
//        $this->assignTitle('注册');
//        if(IS_POST) {
//            $nickname=$this->_param('mobile');
//            $username=$this->_param('mobile');
//            $password=$this->_param('password');
//            $code=$this->_param('code');
//            $invite_code=$this->_param('invite_code');
//            $email=$this->_param('email','');
//            $wxopenid=$this->userinfo['info']['openid'];
//            $result = UserApi::register($nickname,$username,$password,$email,$code,$invite_code,$wxopenid);
//            if (!$result['status']) {
//                $this->error('注册失败','');
//            }else{
//                session("memberinfo",$result['info']);
//                $this->success('注册成功', '');
//            }
//        }
//        return $this->fetch();
//    }
//
//    public function getcode(){
//        $code_type=1;
//        $mobile=$this->_param('mobile');
//        $result = UserApi::code($mobile,$code_type);
//        if($result['status']) {
//            $this->success('短信已发送', '',$result['info']);
//        }else{
//            $this->error('发送失败', '',$result['info']);
//        }
//
//    }
//    public function login(){
//        $this->assignTitle('登录');
//        if (session("?memberinfo")) {
//            $this->redirect('index/index');
//        }
//        if(IS_POST) {
//            $mobile = $this->_param('mobile');
//            $password = $this->_param('password');
//            $result = UserApi::login($mobile, $password, 'device_id_um','pc','+86');
//            if ($result['status']) {
//                session("memberinfo",$result['info']);
//               $uid =$result['info']['uid'];
//                $openid =$this->userinfo['info']['openid'];
//                (new MemberConfigLogic())->save(['uid'=>$uid],['openid'=>$openid]);
//                $this->success('登录成功', '', $result['info']);
//            } else {
//                $this->error('登录失败', '', $result['info']);
//            }
//        }
//        return $this->fetch();
//    }
//


}